package il.ac.tau.cvws.scrabblesidekick.client.ui.service;

import il.ac.tau.cvws.scrabblesidekick.client.R;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.dialog.DialogContent;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class UIServices
{
	/* Creates a dialog box with a progress bar and the given message written in it.
	 */
	public static Dialog createProgressDialog(final Context context, String text)
	{
		Dialog dialog = new Dialog(context,  R.style.FullHeightDialog);
		
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.CENTER);
		
		ProgressBar bar = new ProgressBar(context, null, android.R.attr.progressBarStyle);
		bar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		bar.setPadding(5, 10, 5, 10);
		
		LinearLayout innerLayout = new LinearLayout(context);
		innerLayout.setOrientation(LinearLayout.VERTICAL);
		innerLayout.setGravity(Gravity.CENTER);
		innerLayout.setBackgroundColor(context.getResources().getColor(R.color.transpernt_white));
		innerLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		TextView textView = new TextView(context);
		textView.setText(text);
		textView.setTextSize(16);
		textView.setTextColor(Color.BLACK);
		textView.setPadding(10, 10, 10, 10);
		
		innerLayout.addView(textView);

		layout.addView(bar);
		layout.addView(innerLayout);

		dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		return dialog;
	}
	
	/* Shows a dialog box with the given title and message texts.
	 */
 	public static void showDialog(final Context context, String title, String text, OnCancelListener onCancelListener)
 	{	  
 		Dialog dialog = new Dialog(context,  R.style.FullHeightDialog);
 		dialog.setContentView(R.layout.dialog_tooltip);
 		
 		ImageView imageView = (ImageView)dialog.findViewById(R.id.icon);
 		imageView.setImageResource(R.drawable.s_ninja);
 		
 		TextView textView = (TextView)dialog.findViewById(R.id.text);
 		textView.setText(text);
 		
 		Button okBtn = (Button)dialog.findViewById(R.id.ok_btn);
 		okBtn.setOnClickListener(new UIServices().new CancelButtonListener(dialog));

 		if (onCancelListener != null)
 		{
 			dialog.setOnCancelListener(onCancelListener);
 		}
 		
 		dialog.show();
 	}

 	/* Displays a dialog with the given view as an inner view.
 	 */
 	public static void showViewBasedDialog(final Context context, DialogContent view, OnCancelListener onCancelListener)
 	{	  
 		Dialog dialog = new Dialog(context, R.style.FullHeightDialog_1);
 		dialog.setContentView(view);
 		
 		view.setDialog(dialog);
 		
 		if (onCancelListener != null)
 		{
 			dialog.setOnCancelListener(onCancelListener);
 		}
 		
 		
 		dialog.show();
 	}
 	
 	/* Listener class for a cancel button.
 	 */
 	private class CancelButtonListener implements View.OnClickListener
 	{
 		private Dialog _dialog;
 		
 		public CancelButtonListener(Dialog dialog)
 		{
 			_dialog = dialog;
 		}

		@Override
		public void onClick(View v)
		{
			_dialog.cancel();			
		}
 	}
}
